//
//  AppDelegate.m
//  GoodFood
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()
@property (nonatomic, strong) BMKMapManager *mapManager;
@property (nonatomic, strong) ZWIntroductionViewController *introductionView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    
    [self getSessionId];
    [self mapManage];
//    GFHomeViewController *homeVC = [[GFHomeViewController alloc] init];
//    GFMainNavigationController *mainNavi = [[GFMainNavigationController alloc] initWithRootViewController:homeVC];
//    GFDramViewController *dramVC = [[GFDramViewController alloc] init];
//    self.sideVC = [[YRSideViewController alloc] initWithNibName:nil bundle:nil];
//    self.sideVC.rootViewController = mainNavi;
//    self.sideVC.leftViewController = dramVC;
//    self.sideVC.leftViewShowWidth = ScreenWidth / 375.0 * 300;
//    self.window.rootViewController = self.sideVC;
#pragma mark - 设置引导页
    
    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        //        NSLog(@"第一次启动");
        
        NSString *str = @"day";
        
        [str writeToFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"night.txt"]atomically:YES encoding:NSUTF8StringEncoding error:nil];
        
        
        NSArray *coverImageNames = @[@"touming", @"touming", @"touming"];
        NSArray *backgroundImageNames = @[@"dao1", @"dao2", @"dao3"];
        
        self.introductionView = [[ZWIntroductionViewController alloc] initWithCoverImageNames:coverImageNames backgroundImageNames:backgroundImageNames];
        
        self.window.rootViewController = self.introductionView;
        [self.window addSubview:self.introductionView.view];
        
        
        __weak AppDelegate *weakSelf = self;
        self.introductionView.didSelectedEnter = ^() {
            [weakSelf.introductionView.view removeFromSuperview];
            weakSelf.introductionView = nil;
            
            
            
#pragma mark - 进入主程序
            
            //            [[DataBaseHandle shareDataBaseHandle] openDB];
            
            GFHomeViewController *homeVC = [[GFHomeViewController alloc] init];
            GFMainNavigationController *mainNavi = [[GFMainNavigationController alloc] initWithRootViewController:homeVC];
            GFDramViewController *dramVC = [[GFDramViewController alloc] init];
            self.sideVC = [[YRSideViewController alloc] initWithNibName:nil bundle:nil];
            self.sideVC.rootViewController = mainNavi;
            self.sideVC.leftViewController = dramVC;
            self.sideVC.leftViewShowWidth = ScreenWidth / 375.0 * 300;
            self.window.rootViewController = self.sideVC;
        
        };
    }else {
        //        NSLog(@"已经不是第一次启动了");
        //        [[DataBaseHandle shareDataBaseHandle] openDB];
        GFHomeViewController *homeVC = [[GFHomeViewController alloc] init];
        GFMainNavigationController *mainNavi = [[GFMainNavigationController alloc] initWithRootViewController:homeVC];
        GFDramViewController *dramVC = [[GFDramViewController alloc] init];
        self.sideVC = [[YRSideViewController alloc] initWithNibName:nil bundle:nil];
        self.sideVC.rootViewController = mainNavi;
        self.sideVC.leftViewController = dramVC;
        self.sideVC.leftViewShowWidth = ScreenWidth / 375.0 * 300;
        self.window.rootViewController = self.sideVC;
    }

    
    return YES;
}
#pragma mark - 地图管理
- (void)mapManage {
    
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8) {
        //由于IOS8中定位的授权机制改变 需要进行手动授权
        CLLocationManager  *locationManager = [[CLLocationManager alloc] init];
        //获取授权认证
        [locationManager requestAlwaysAuthorization];
        [locationManager requestWhenInUseAuthorization];
    }

    self.mapManager = [[BMKMapManager alloc]init];
    BOOL ret = [self.mapManager start:@"U8f1Wl55lB0WtF6jADf2xsyk" generalDelegate:nil];
    
    if (!ret) {
        NSLog(@"manager start failed!");
    } else {
        
    }
}
#pragma mark - 进入app的sessionId获取
- (void)getSessionId {
    NSString *url = @"http://api.meishixing.com/other/config/config";
    [AFNetHandle GETWithURL:url body:nil httpHead:nil responseStyle:JSON success:^(id result) {
        NSDictionary *dictionary = [NSDictionary dictionaryWithDictionary:result];
        NSDictionary *dic = [dictionary objectForKey:@"result"];
        self.session_id = [dic objectForKey:@"session_id"];
        NSLog(@"jdjjajjdja%@",self.session_id);
    } fail:^(NSError *error) {
        
    }];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    [BMKMapView willBackGround];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [BMKMapView didForeGround];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
